<?php

namespace App\Console;

use App\Libs\SmsClient;
use App\Libs\WeChat\CodeClient;
use App\Libs\WeChat\WxSmallClient;
use App\Libs\WeChat\WxSmsClient;
use App\Models\order_info;
use App\Models\user_formid;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Ixudra\Curl\Facades\Curl;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        'App\Console\Commands\MakeCode',
        'App\Console\Commands\ExportAld'
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            try {
                $where = [];
                $where['order_info.order_status'] = 2;
                $where['goods.goods_type'] = 2;
                $datas = order_info::leftjoin('goods', "order_info.goods_id", "=", "goods.id")->where($where)->orderBy('order_info.id', 'DESC')->select('order_info.id', 'order_number', 'goods_id', 'price_sum', 'user_id', 'create_time', 'order_status', 'title', 'original_price', 'retail_price', 'discount_price', 'goods_num', 'goods_type', 'goods_cover', 'card_id', 'create_time')->get();
                if ($datas) {
                    $template_id = '3Mx8M_1-0d9LykW5dWvhp2-NKqFUeckf11YzyTLtUr4';
                    //示例数据根据消息模板填充
                    $data = array(
                        'keyword3' => array('value' => '您有一张已购买的优惠券还未领取，请点击消息打开小程序领取', 'color' => '#7167ce'),
                        'keyword4' => array('value' => '本宫PavoMea', 'color' => '#7167ce'),
                    );
                    foreach ($datas as $v) {
                        $data['keyword1'] = array('value' => $v['create_time'], 'color' => '#7167ce');
                        $data['keyword2'] = array('value' => $v['title'], 'color' => '#7167ce');
                        $res = Redis::sismember('push_order', $v['id']);
                        savelog('redis_order', '&order_id=' . $v['id'] . '结果：' . json_encode($res));
                        if (!$res) {
                            //存入集合中
                            Redis::sadd('push_order', $v['id']);
                            $formId = user_formid::lastformid($v['user_id']);
                            if (!$formId) {
                                savelog('no_push_user', 'openid=' . $v['user_id'] . '&order_id=' . $v['id']);
                            }
                            if (time() - strtotime($v['create_time']) > 15 * 60) {
                                continue;
                            }
                            $res = WxSmallClient::sendTemplate($v['user_id'], $data, $formId->form_id, $template_id, 'pages/buy/buy?card_id=' . $v['card_id'] . '&order_id=' . $v['id']);
                            if (!isset($res['errcode']) || $res['errcode'] != 0) {
                                savelog('push_err_log', json_encode($res) . 'openid=' . $v['user_id'] . '&form_id=' . $formId->form_id);
                            }
                            // 删除用过的form_id
                            user_formid::where('id', $formId->id)->delete();
                            echo 'ok';
                        }
                    }
                }
            } catch (\Exception $ex) {
                savelog('export_ald_log', $ex->getMessage() . $ex->getLine());
            }
        })->cron('*/1 * * * *');
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}
